package com.wyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyh.common.response.PageInfo;
import com.wyh.common.result.R;
import com.wyh.common.util.SnowUtil;
import com.wyh.entity.MsgChatRoomRecord;
import com.wyh.mapper.MsgChatRoomRecordMapper;
import com.wyh.request.MsgChatRoomRecordRequest;
import com.wyh.service.IMsgChatRoomRecordService;
import com.wyh.service.IMsgChatRoomService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 【请填写功能名称】业务层处理
 *
 * @author WangYuHang
 * @date 2024-03-08
 */
@Slf4j
@Service
@AllArgsConstructor
public class MsgChatRoomRecordServiceImpl implements IMsgChatRoomRecordService {

    private final MsgChatRoomRecordMapper baseMapper;


    @Autowired
    private IMsgChatRoomService iMsgChatRoomService;

    @Autowired
    private SnowUtil snowUtil;
    /**
     * 发送群聊消息
     * @param msgChatRoomRecordRequest
     * @return
     */
    @Override
    public R<String> sendChatRoomMessage(MsgChatRoomRecordRequest msgChatRoomRecordRequest) {

        //判断是否在群里
        if (!iMsgChatRoomService.queryIsExist(msgChatRoomRecordRequest.getFromId(), msgChatRoomRecordRequest.getToChatRoomId())){
            R.error("未在群里,发送失败");
        }

        long l = snowUtil.generateId();
        MsgChatRoomRecord build = MsgChatRoomRecord.builder().messageId(l).build();
        BeanUtils.copyProperties(msgChatRoomRecordRequest,build);

        baseMapper.insert(build);
        return R.success("群消息发送成功！");
    }

    @Override
    public R<List<MsgChatRoomRecord>> queryMessage(String chatRoomId) {


        //以后需要用分页查询查最近的10条
        List<MsgChatRoomRecord> msgChatRoomRecords = baseMapper.selectList(Wrappers.lambdaQuery(MsgChatRoomRecord.class).eq(MsgChatRoomRecord::getToChatRoomId, chatRoomId));



        return R.success(msgChatRoomRecords);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MsgChatRoomRecord add(MsgChatRoomRecordRequest request) {
        MsgChatRoomRecord entity = MsgChatRoomRecord.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(MsgChatRoomRecordRequest request) {
        MsgChatRoomRecord entity = MsgChatRoomRecord.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public MsgChatRoomRecord getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<MsgChatRoomRecord> pageList(MsgChatRoomRecordRequest request) {
        Page<MsgChatRoomRecord> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<MsgChatRoomRecord> query = this.buildWrapper(request);
        IPage<MsgChatRoomRecord> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<MsgChatRoomRecord> list(MsgChatRoomRecordRequest request) {
        LambdaQueryWrapper<MsgChatRoomRecord> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<MsgChatRoomRecord> buildWrapper(MsgChatRoomRecordRequest param) {
        LambdaQueryWrapper<MsgChatRoomRecord> query = new LambdaQueryWrapper<>();

         if (StringUtils.isNotBlank(param.getFromId())){
               query.like(MsgChatRoomRecord::getFromId ,param.getFromId());
        }
         if (StringUtils.isNotBlank(param.getToChatRoomId())){
               query.like(MsgChatRoomRecord::getToChatRoomId ,param.getToChatRoomId());
        }
        if (param.getType() != null){
               query.eq(MsgChatRoomRecord::getType ,param.getType());
        }
         if (StringUtils.isNotBlank(param.getContent())){
               query.like(MsgChatRoomRecord::getContent ,param.getContent());
        }
         if (param.getSendTime() != null){
               query.like(MsgChatRoomRecord::getSendTime ,param.getSendTime());
        }
        if (param.getIsDelete() != null){
               query.eq(MsgChatRoomRecord::getIsDelete ,param.getIsDelete());
        }
         if (StringUtils.isNotBlank(param.getCreateUserId())){
               query.like(MsgChatRoomRecord::getCreateUserId ,param.getCreateUserId());
        }
         if (StringUtils.isNotBlank(param.getUpdateUserId())){
               query.like(MsgChatRoomRecord::getUpdateUserId ,param.getUpdateUserId());
        }
        return query;
    }

}
